package com.yueke.gemini.utils;

import jxl.CellView;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.*;

import java.awt.*;
import java.awt.Label;
import java.io.File;

/**
 * Created by admin on 2017/2/24.
 */
public class JXLUtil {

    public static void main(String[] args) throws Exception {
        String title ="报表测试";
        String[] navTitle= {"第一行","第二行","第三行","第四行","第五行","第六行","第七行","第八行"};
        String[][] content={
                {"1","2","第naionfdapfn三行","第四niaodnfoanfdas行","第noandfoasnjdf五行","第六sdfadsafas行","第afdadfasdfs七a行","第adfasfdasf八行"},
                {"2","2","第三行","第四行","第五行","第六行","第七行","sssssssssss第八sss行"},
                {"3","2","第三行","第四行","第五行","第六行","第七行","第八行sssssssssssss"},
                {"4","2","第三行","第四行","第sssssssssssssss五行","第ssssssssssssssssssss六行","第七行","第八行sssssssss"},
                {"5","2","第三行","第ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd四行","第五行","第六行","第七行","第八行"},
                {"6","2","第三行","第四行","第五行","第六行","第七行","第八行"},
                {"7","2","第三行","第四ddddddddddddddddddddddddddddddd行","第五行","第六行","第七行","第八行"},
                {"8","2","第三行","第四行","第五行","第六行","第七行","第八行"},
                {"9","2","第三行","第ddddddddddddddddddddddddddddddd四行","第五行","第六行","第七行","第八行"},
                {"10","2","第三行","第四行","第五行","第六行","第七行","第八行"},
                {"11","2","第三行","第四行","第五行","第六dddddddddddddd行","第七行","第八行"},
                {"12","2","第三行","第四行","第五行","第六行","第七行","第八行"},
                {"13","2","第三行","第四行","第五行","dddddddddddddddddddddd第六行","第七行","第八行"},
                {"14","2","第三行","第四行","第五行","第dddddddddddddddddddddd六行","第七行","第八行"}
        };
        /**
         * 文件路径
         */
        String filePath="D:\\DesignSource\\tempT";
        String fileName="NewProject.xls";
        File dir=new  File(filePath);
        if(!dir.isDirectory()){
            dir.mkdirs();
        }

        File file = new File(filePath+"\\"+fileName);
        WritableWorkbook workbook = Workbook.createWorkbook(file);
        WritableSheet sheet = workbook.createSheet("报表统计", 0);  //单元格

        /**
         * title
         */
        java.awt.Label lab = null;
        WritableFont wf2  = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK); // 定义格式 字体 下划线 斜体 粗体 颜色
        WritableCellFormat wcfTitle = new WritableCellFormat(wf2);
        wcfTitle.setBackground(jxl.format.Colour.IVORY);  //象牙白
        wcfTitle.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //BorderLineStyle边框
        wcfTitle.setVerticalAlignment(VerticalAlignment.CENTRE); //设置垂直对齐
        wcfTitle.setAlignment(Alignment.CENTRE); //设置垂直对齐

//        CellView navCellView = new CellView();
//        navCellView.setAutosize(true); //设置自动大小
//        navCellView.setSize(18);
//
//        lab = new Label(0,0,title,wcfTitle); //Label(col,row,str);
//        sheet.mergeCells(0,0,navTitle.length-1,0);
//        sheet.setColumnView(0, navCellView); //设置col显示样式
////        sheet.setRowView(0, 1600, false); //设置行高
//        sheet.addCell(lab);

        // 宽度自适应。能够根据内容增加宽度，但对中文的支持不好，如果内容中包含中文，会有部分内容被遮盖
        for (int i = 0; i < navTitle.length; i++) {
            CellView cell = sheet.getColumnView(i);
            cell.setAutosize(true);
            sheet.setColumnView(i, cell);
        }

        /**
         *  navTitle
         */
        jxl.write.WritableFont wfcNav =new jxl.write.WritableFont(WritableFont.ARIAL,12, WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
        WritableCellFormat wcfN=new WritableCellFormat(wfcNav);

        Color color = Color.decode("#0099cc"); // 自定义的颜色
        workbook.setColourRGB(Colour.ORANGE, color.getRed(),color.getGreen(), color.getBlue());
        wcfN.setBackground(Colour.ORANGE);
        wcfN.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //BorderLineStyle边框
        wcfN.setAlignment(Alignment.CENTRE); //设置水平对齐
        wcfN.setWrap(false); //设置自动换行

        CellView cell = new CellView();
        cell.setAutosize(true); //设置自动大小
//        cell.setSize(18);


        for(int i=0;i<navTitle.length;i++){
//            lab = new java.awt.Label(i,1,navTitle[i],wcfN); //Label(col,row,str);
//            sheet.addCell(lab);

//            sheet.setColumnView(i, cell);//根据内容自动设置列宽
//            sheet.setColumnView(i, new String(navTitle[i]).length());

//            byte[] bstrLength = navTitle[i].getBytes(); //中文字符算两个字节
//            sheet.setColumnView(i, bstrLength.length+2);
        }

        /**
         * 内容
         */
        jxl.write.WritableFont wfcontent =new jxl.write.WritableFont(WritableFont.ARIAL,12, WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN);
        WritableCellFormat wcfcontent = new WritableCellFormat(wfcontent);
        wcfcontent.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //BorderLineStyle边框
        wcfcontent.setAlignment(Alignment.CENTRE);


        for(int i=0;i<content.length;i++){
            for(int j=0;j<content[i].length;j++){
//                lab = new Label(j,i+2,content[i][j],wcfcontent); //Label(col,row,str);
//                sheet.addCell(lab);
//                sheet.setColumnView(i, cell);//根据内容自动设置列宽
//                sheet.setColumnView(j, new String(content[i][j]).length());
//                sheet.setRowView(2,2, false);

            }
        }

        workbook.write();
        workbook.close();
    }

}
